<?php

class RequestController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl',
			'ajaxOnly'
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array(
					'suggestEvent',
					'suggestEventLabel',
					'suggestNews',
					'deleteContact',
					'deleteTicket',
                    'suggestTag',
                    'artistsAddSocial',
                    'organizersAddSocial',
                    'deleteImageArtists',
                    // Places ajax request
                    'placeAddSocial',
					'placeDelPlaceImage',
				),
				'users'=>array('*'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionDeleteImageArtists()
	{
		If (Yii::app()->request->isAjaxRequest)
		{
			$model = ArtistImages::model()->findByPk($_POST['id']);
			if ($model === null)
				echo "success";
			$nameImage = $model->image; 
			if ($model->delete())
			{
				$imagePath = IMAGES_DIR . 'Artists/';
				if (file_exists($imagePath.$nameImage))
					unlink($imagePath.$nameImage);
				echo "success";
			}
				
			else
				echo "error";
		}
	}

	public function actionSuggestEvent($term)
	{
		$model = Events::model()->findAll('title LIKE "%' . $term . '%" AND status="A" AND enddate >= NOW() ORDER BY posteddate DESC');
		$result = array();
		if (!empty($model)) {
			foreach ($model as $event) {
				$result[] = array('label'=>$event->title, 'value'=>$event->id . ' - ' . $event->title, 'id'=>$event->id, 'image'=>IMAGES_URL . 'events/' . date('Ymd', strtotime($event->posteddate)) . '/' . $event->image);
			}
		}
		
		echo CJSON::encode($result);
	}
	
	public function actionSuggestNews($term)
	{
		$model = News::model()->findAll('title LIKE "%' . $term . '%" AND status="A" ORDER BY posteddate DESC');
		$result = array();
		if (!empty($model)) {
			foreach ($model as $data) {
				$result[] = array('label'=>$data->title, 'value'=>$data->id . ' - ' . $data->title, 'id'=>$data->id, 'image'=>IMAGES_URL . 'news/' . date('Ymd', strtotime($data->posteddate)) . '/' . $data->image);
			}
		}
		
		echo CJSON::encode($result);
	}
	
	public function actionSuggestEventLabel($term)
	{
		$model = Events::model()->findAll('title LIKE "%' . $term . '%" AND status="A" ORDER BY posteddate DESC');
		$result = array();
		if (!empty($model)) {
			foreach ($model as $event) {
				$result[] = array('label'=>$event->title, 'value'=>$event->id . ' - ' . $event->title, 'id'=>$event->id, 'image'=>IMAGES_URL . 'events/' . date('Ymd', strtotime($event->posteddate)) . '/' . $event->image);
			}
		}
		
		echo CJSON::encode($result);
	}
    
    
    // Placescontroller ajax add Social
    public function actionPlaceAddSocial()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $model = new PlaceSocial;
            echo $this->renderPartial('placeSocial', array('model'=>$model, 'i'=>$_POST['i']), false, true);
        }
    }

     //  ajax add Social
    public function actionArtistsAddSocial()
    {
        if (Yii::app()->request->isAjaxRequest)
        {
            $model = new ArtistSocial;
            echo $this->renderPartial('artistsSocial', array('model'=>$model, 'i'=>$_POST['i']), false, true);
        }
    }
	
	public function actionOrganizersAddSocial(){
		if (Yii::app()->request->isAjaxRequest)
        {
            $model = new OrganizerSocial;
            echo $this->renderPartial('organizersSocial', array('model'=>$model, 'i'=>$_POST['i']), false, true);
        }
	}
	// del place image ajax
	public function actionPlaceDelPlaceImage()
	{
		If (Yii::app()->request->isAjaxRequest)
		{
			$model = PlaceImages::model()->findByPk($_POST['id']);
			if ($model === null)
				echo "success";
			$nameImage = $model->image;
			$createddate = $model->placesId->createddate;
			if ($model->delete())
			{
				$imagePath = IMAGES_DIR . 'places/' . date('Ymd', strtotime($createddate)) . '/';
				if (file_exists($imagePath.$nameImage))
					unlink($imagePath.$nameImage);
				echo "success";
			}
				
			else
				echo "error";
		}
	}

public function actionSuggestTag($q)
    {
        $criteria = new CDbCriteria();
		$criteria->compare('title', trim($q), true);
		$criteria->order = 'posteddate DESC';
		
		$model = Labels::model()->findAll($criteria);
        
        $result = array();
        foreach ($model as $data) {
            $result[] = array('id'=>$data->id, 'text'=>$data->title);
        }
        
        echo CJSON::encode($result);
    }
	
	public function actionDeleteContact()
	{
		$model = EventContact::model()->findByPk($_POST['id']);
		
		if (!empty($model)) {
			$model->delete();
		}
	}
	
	public function actionDeleteTicket()
	{
		$model = EventTickets::model()->findByPk($_POST['id']);
		
		if (!empty($model)) {
			$model->delete();
		}
	}
}